﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Expansion
{
    public static class ObjectEx
    {
        /// <summary>
        /// 转换为Int，默认值：0
        /// </summary>
        public static int TryInt(this Object strText)
        {
            return TryInt(strText, 0);
        }

        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static int TryInt(this Object strText, int defValue)
        {
            int temp = -9999;
            return int.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Long，默认值：0
        /// </summary>
        public static long TryLong(this Object strText)
        {
            return TryLong(strText, 0);
        }

        /// <summary>
        /// 转换为Long
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static long TryLong(this Object strText, long defValue)
        {
            long temp = -9999;
            return long.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Double，默认值：0
        /// </summary>
        public static double TryDouble(this Object strText)
        {
            return TryDouble(strText, 0.0);
        }

        /// <summary>
        /// 转换为Double
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static double TryDouble(this Object strText, double defValue)
        {
            double temp = -9999.0;
            return double.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Boolean，默认值：false
        /// </summary>
        public static Boolean TryBoolean(this Object strText)
        {
            return TryBoolean(strText, false);
        }

        /// <summary>
        /// 转换为Boolean
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static Boolean TryBoolean(this Object strText, bool defValue)
        {
            bool temp = false;
            return bool.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Decimal，默认值：0
        /// </summary>
        public static Decimal TryDecimal(this Object strText)
        {
            return TryDecimal(strText, 0);
        }

        /// <summary>
        /// 转换为Decimal
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static Decimal TryDecimal(this Object strText, decimal defValue)
        {
            decimal temp = -9999;
            return decimal.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为DateTime，默认值：1900-1-1
        /// </summary>
        public static DateTime TryDateTime(this Object strText)
        {
            return TryDateTime(strText, DateTime.Parse("1900-1-1"));
        }

        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static DateTime TryDateTime(this Object strText, DateTime defValue)
        {
            DateTime temp = DateTime.Parse("1900-1-1");
            return DateTime.TryParse(strText + "", out temp) ? temp : defValue;
        }



        /// <summary>
        /// 为NULL 和 DBNull的返回String.Empty
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string TryString(this Object str)
        {
            return TryString(str, "");
        }

        /// <summary>
        /// 转换为""
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string TryString(this Object str, string defvalue)
        {
            return str == null ? defvalue : str.ToString();
        }
    }
}
